package com.echoesnet.echoesshortvideo.ViewModels;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.util.ArrayMap;

import com.echoesnet.echoesshortvideo.Api.Subscriber.SilenceSubscriber;
import com.echoesnet.echoesshortvideo.Beans.FansFocusBean;
import com.echoesnet.echoesshortvideo.Persistence.BlackListBean;
import com.echoesnet.echoesshortvideo.Repositories.FansAndFocusRepository;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 * 负责驱动UI
 *
 * @author lc
 * @version 1.0
 * @modifier
 * @createDate 2018/4/19 18:05
 * @description
 */
public class FansFocusViewModel extends ViewModel
{
    private MutableLiveData<Map<String,Object>> followDetail=  new MutableLiveData<>();
    private MutableLiveData<Map<String,Object>> fanDetail=  new MutableLiveData<>();
    private MutableLiveData<String> followResult=  new MutableLiveData<>();

    private MutableLiveData<List<BlackListBean>> blackDetail = new MutableLiveData<>();

    private MutableLiveData<BlackListBean> removeBlack = new MutableLiveData<>();
    private MutableLiveData<String> reportUserBack = new MutableLiveData<>();
    private MutableLiveData<FansFocusBean> followBeanResult = new MutableLiveData<>();
    private MutableLiveData<Map<String,Object>> focusListBlack = new MutableLiveData<>();
    private FansAndFocusRepository uDetailRepository;
    private int index = 0;
    private int num = 15;
    public LiveData<Map<String,Object>> getFollowDetail()
    {
        return followDetail;
    }

    public LiveData<Map<String,Object>> getFanDetail()
    {
        return fanDetail;
    }
    public LiveData<String> getResultDetail()
    {
        return followResult;
    }


    public LiveData<FansFocusBean> getFollowBack()
    {
        return followBeanResult;
    }
   // public LiveData<FansFocusBean> getFollowDetail()

    public LiveData<List<BlackListBean>> getBlackDetail()
    {
        return blackDetail;
    }

    public LiveData<String> reportUser()
    {
        return reportUserBack;
    }


    public LiveData<BlackListBean> getRemoveBack()
    {
        return removeBlack;
    }

    public LiveData<Map<String,Object>> getFocusListBack()
    {
        return focusListBlack;
    }


    // public LiveData<FansFocusBean> getFollowDetail()
//    {
//        return fanDetail;
//    }


    public FansFocusViewModel()
    {
        this.uDetailRepository = new FansAndFocusRepository();
    }

    /**
     * 查询关注列表
     *
     * @param tId 用户tId
     */
    public void requestFollowList(String tId,String isRefresh)
    {
        index = 0;
        uDetailRepository.getFollowList(tId, index, num, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                List<FansFocusBean> list= new Gson().fromJson(response, new TypeToken<List<FansFocusBean>>()
                {
                }.getType());
                Map<String,Object> map = new HashMap<>();
                map.put("isRefresh",isRefresh);
                map.put("List",list);
                map.put("hasMore",list.size()>0);

                followDetail.setValue(map);
            }
        });
    }
    /**
     * 查询关注列表加载更多
     *
     * @param tId 用户tId
     */
    public void requestFollowMoreList(String tId,String isRefresh)
    {
        index += num;
        uDetailRepository.getFollowList(tId, index, num, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                List<FansFocusBean> list= new Gson().fromJson(response, new TypeToken<List<FansFocusBean>>()
                {
                }.getType());
                Map<String,Object> map = new HashMap<>();
                map.put("isRefresh",isRefresh);
                map.put("hasMore",list.size()>0);
                map.put("List",list);

                followDetail.setValue(map);
            }
        });
    }

    /**
     * 查询粉丝列表
     *
     * @param tId 用户tId
     */
    public void requestFansList( String tId,String isRefresh)
    {
        index = 0;
        uDetailRepository.getFansList(tId, index, num, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                List<FansFocusBean> list= new Gson().fromJson(response, new TypeToken<List<FansFocusBean>>()
                {
                }.getType());
                Map<String,Object> map = new HashMap<>();
                map.put("isRefresh",isRefresh);
                map.put("hasMore",list.size()>0);
                map.put("List",list);

                fanDetail.setValue(map);

            }
        });
    }

    /**
     * 查询粉丝列表加载更多
     *
     * @param tId 用户tId
     */
    public void requestFansMoreList( String tId,String isRefresh)
    {
        index += num;
        uDetailRepository.getFansList(tId, index, num, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                List<FansFocusBean> list= new Gson().fromJson(response, new TypeToken<List<FansFocusBean>>()
                {
                }.getType());
                Map<String,Object> map = new HashMap<>();
                map.put("isRefresh",isRefresh);
                map.put("hasMore",list.size()>0);
                map.put("List",list);

                fanDetail.setValue(map);

            }
        });
    }


    /**
     * 查询黑名单列表
     */
    public void requestBlackList(int index, int num)
    {
        uDetailRepository.getBlackList(index, num, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                List<BlackListBean> list= new Gson().fromJson(response, new TypeToken<List<BlackListBean>>()
                {
                }.getType());
                blackDetail.setValue(list);

            }
        });
    }

    /**
     * 关注
     *
     * @param otherId 用户id
     */
    public void requestFollow(String otherId)
    {
        uDetailRepository.follow(otherId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject jsonObject = new JSONObject(response);
                    followResult.setValue((jsonObject.getString("status")));
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }


            }
        });
    }
    /**
     *  关注粉丝列表里面用的，
     * 关注
     */
    public void requestFollowBean(String userId,int position)
    {
        uDetailRepository.follow(userId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    Map<String,Object> map = new HashMap<>();

                    map.put("position",position);
                    map.put("status",new JSONObject(response).getString("status"));
                    focusListBlack.setValue(map);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }




            }
        });
    }
    /**
     * 关注粉丝列表里面用的，
     * 取消关注
     * @param
     */
    public void requestCancelFollowBean(FansFocusBean bean,final boolean isfocus)
    {
        uDetailRepository.follow(bean.getUserId(), new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);

                followBeanResult.setValue(bean);
            }
        });
    }





    /**
     * 拉黑
     *
     * @param otherId 用户id
     */
    public void requestPullBlack(String otherId)
    {
        uDetailRepository.pullBlack(otherId,new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                followResult.setValue("pullBlack");
            }
        });
    }

    /**
     * 移除黑名单
     * @param otherId 用户bean
     */
    public void removeBlack(String otherId)
    {
        uDetailRepository.removeBlack(otherId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                followResult.setValue("removeBlackok");
            }
        });
    }

    /**
     * 移除黑名单
     * @param bBean 用户bean
     */
    public void removeBlack(BlackListBean bBean)
    {
        uDetailRepository.removeBlack(bBean.getUserId(), new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);

                removeBlack.setValue(bBean);
            }
        });
    }

    /**
     * 举报
     *
     * @param otherId 用户id
     */
    public void requestReportUser(String otherId,String reason,String content)
    {
        uDetailRepository.requestReportUser(otherId,reason,content,new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                reportUserBack.setValue(response);
            }
        });
    }
}
